/**
 * 具体的资源池信息
 */
$(function () {

  // 获取url参数
  var urlParams = getQueryVariable();

    // 获取参数
    function getQueryVariable() {
        var url = window.location.search; //获取url中"?"符后的字串
        url = decodeURI(url);
        var paramsJson = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                paramsJson[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return paramsJson;
    }

    // 收藏
    $("#collection").on("click",function(){
        var isLogin = checkLogin();
        if (isLogin) {
          var myUrl = window.location.href;
          try {
            window.external.addFavorite(myUrl, "eqweq");
          }
          catch (e) {
            try {
              window.sidebar.addPanel("eqweq", myUrl, "");
            }
            catch (e) {
              alert("抱歉，您所使用的浏览器无法完成此操作。\n\n加入收藏失败，请使用Ctrl+D进行添加");
            }
          }
        } else {
            $("#login_btn").click();
        }
    })

    // 报名
    $("#meeting").on("click",function(){
        var exStatus = urlParams["exStatus"];
        if(exStatus != "报名中"){
            showTip();
            return;
        }
        var isLogin = checkLogin();
        if (isLogin) {
            resetFormData();
            $('#myModal').modal('show');
        } else {
            $("#login_btn").click();
        }

    })
    function resetFormData(){
        $("#fcn").val("");
        $("#fcp").val("");
        $("#fcm").val("");
        $("#fca").val("");
        $("#appNote").val("");
    }

    // 显示提示
   function showTip(){
       $("#tipModal").modal('show');
    }
   function hideTip(){
       $("#tipModal").modal('hide');
    }
    $("#hideTip").on("click",function(){
        hideTip();
    })

    // 确认报名
    $("#confirm").on("click",function(){
        var userInfo = getUserInfo();
        let params = {
            type: "post",
            contentType: "application/x-www-form-urlencoded",
            headers: {token: userInfo.token},
            data: getFormData(),
            url: "/index/activity/enroll",
            sCallback: function (data) {
                saveSuccessCallback(data)
            },
            eCallback: function (res) {
                errorSuccessCallback(res)
            }
        }
        requset(params);
    })
    function getFormData(){
        var exId = urlParams["id"];
        var name = $("#name").val();
        var workplace = $("#workplace").val();
        var job = $("#job").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var addr = $("#addr").val();
        var memo = $("#memo").val();
        var userInfo = getUserInfo();
        var loginUserId = "df57d282-8fd5-11e8-a2d4-00163e0280a5";
        return {exId:exId,name:name,workplace:workplace,job:job,phone:phone,email:email,addr:addr,memo:memo,loginUserId:loginUserId}
    }
    function saveSuccessCallback(data){
        $("#result_content").text('操作成功！');
        $("#resultModal").modal('show');
        $("#myModal").modal('hide');
    }
    function errorSuccessCallback(res){
        $("#result_content").text('操作失败！');
        $("#resultModal").modal('show');
    }
    $("#result_btn").on("click",function() {
        $("#resultModal").modal('hide');
    })
    // 取消报名
    $("#cancel").on("click",function(){
        $("#myModal").modal('hide');
    })
});


